import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
import _objectWithoutProperties from "@babel/runtime/helpers/esm/objectWithoutProperties";
var _excluded = ["name", "children", "ignoreFormListField"];
import { jsx as _jsx } from "react/jsx-runtime";
import { isDeepEqualReact, merge, ProFormContext } from '@ant-design/pro-utils';
import { Form } from 'antd-v4';
import get from "rc-util/es/utils/get";
import set from "rc-util/es/utils/set";
import { useContext, useMemo } from 'react';
import { FormListContext } from '../List';

var ProFormDependency = function ProFormDependency(_ref) {
  var names = _ref.name,
      _children = _ref.children,
      ignoreFormListField = _ref.ignoreFormListField,
      rest = _objectWithoutProperties(_ref, _excluded);

  var context = useContext(ProFormContext); // ProFromList 的 field，里面有name和key

  var formListField = useContext(FormListContext); // flatten each name into an (string | number)[]

  var flattenNames = useMemo(function () {
    return names.map(function (itemName) {
      var _formListField$listNa;

      var name = [itemName]; // ignoreFormListField为 true 或 formListField.name === undefined 时
      // 应从全局取值，要将 names 中各项的路径前缀(formListField.listName)忽略

      if (!ignoreFormListField && formListField.name !== undefined && ((_formListField$listNa = formListField.listName) === null || _formListField$listNa === void 0 ? void 0 : _formListField$listNa.length)) {
        name.unshift(formListField.listName);
      }

      return name.flat(1);
    });
  }, [formListField.listName, formListField.name, ignoreFormListField, names]);
  return _jsx(Form.Item, _objectSpread(_objectSpread({}, rest), {}, {
    noStyle: true,
    shouldUpdate: function shouldUpdate(prevValues, nextValues, info) {
      if (typeof rest.shouldUpdate === 'boolean') {
        return rest.shouldUpdate;
      } else if (typeof rest.shouldUpdate === 'function') {
        var _rest$shouldUpdate;

        return (_rest$shouldUpdate = rest.shouldUpdate) === null || _rest$shouldUpdate === void 0 ? void 0 : _rest$shouldUpdate.call(rest, prevValues, nextValues, info);
      }

      return flattenNames.some(function (name) {
        return !isDeepEqualReact(get(prevValues, name), get(nextValues, name));
      });
    },
    children: function children(form) {
      var values = {};

      for (var i = 0; i < names.length; i++) {
        var _context$getFieldForm;

        var pathToGet = flattenNames[i],
            pathToSet = names[i];
        var finalName = [pathToSet].flat(1);
        var value = (_context$getFieldForm = context.getFieldFormatValueObject) === null || _context$getFieldForm === void 0 ? void 0 : _context$getFieldForm.call(context, pathToGet);

        if (value && Object.keys(value).length) {
          // transform 会生成多余的value，这里需要注入一下
          values = merge({}, values, value);

          if (get(value, pathToGet)) {
            values = set(values, finalName, get(value, pathToGet), false);
          }
        } else {
          var _form$getFieldValue;

          value = (_form$getFieldValue = form.getFieldValue) === null || _form$getFieldValue === void 0 ? void 0 : _form$getFieldValue.call(form, pathToGet);

          if (typeof value !== 'undefined') {
            values = set(values, finalName, value, false);
          }
        }
      }

      return _children === null || _children === void 0 ? void 0 : _children(values, _objectSpread(_objectSpread({}, form), context));
    }
  }));
};

export default ProFormDependency;